package com.ac.interceptor;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.ac.anotation.Record;
import com.ac.dao.impl.HibernateBaseDaoImpl;
import com.ac.entity.OperateRecord;
import com.ac.entity.User;
import com.ac.util.DataStore;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class AuthorityInterceptor extends AbstractInterceptor {

	private Log log = LogFactory.getLog(this.getClass());
	private HibernateBaseDaoImpl hibernateBaseDao;

	public HibernateBaseDaoImpl getHibernateBaseDao() {
		return hibernateBaseDao;
	}

	public void setHibernateBaseDao(HibernateBaseDaoImpl hibernateBaseDao) {
		this.hibernateBaseDao = hibernateBaseDao;
	}

	public String intercept(ActionInvocation invocation) throws Exception {

		ActionContext ctx = invocation.getInvocationContext();
		ActionSupport action = (ActionSupport) invocation.getAction();
		String actionName = invocation.getProxy().getActionName();
		String methodName = invocation.getProxy().getMethod();
		Map session = ctx.getSession();
		
		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		
		
		request.getParameterMap();

		if (session != null && session.get("user") != null) {
			User user = (User) session.get("user");
			
			//写操作日志
			Record record = action.getClass().getMethod(methodName, null).getAnnotation(Record.class);
			if(record != null){
				
				OperateRecord operateRecord = new OperateRecord();
				
				StringBuffer deail = new StringBuffer();
				Map<String,String[]> map  = request.getParameterMap();
				Iterator<String> i = map.keySet().iterator();
				
				while(i.hasNext()){
					deail.append("{");
					String key = i.next();
					String[] value = map.get(key);
					
					deail.append(key + ":");
					
					if(value != null && value.length == 1){
						deail.append(value[0] );
					}else{
						for (String string : value) {
							deail.append("[" + string + "]");
						}
					}
					deail.append("}");
				}
				
				operateRecord.setDetail(deail.toString());
				operateRecord.setMethod(methodName);
				operateRecord.setClassName(actionName);
				operateRecord.setOperate(record.operate());
				operateRecord.setUserId(user.getId());
				operateRecord.setType(record.type().toString());
				hibernateBaseDao.saveOrUpdate(operateRecord);
			}
			
			
			// 2.
			if (DataStore.getAliasMap().get(actionName) == null) {
				return invocation.invoke();
			} else {
				if (DataStore.getAliasMap().get(actionName)
						.contains(user.getDept() + "")) {

					if (user.getAuthorityUnion().contains(
							getOperateAuthority(methodName))) {
						return invocation.invoke();
					}
					action.addActionError("does not have permission to operate");
					return "error";
				} else {

					action.addActionError("does not have permission to access");
					return "error";
				}
			}

		}
		return "login";
	}

	public static String getOperateAuthority(String methodName) {

		if ("execute".equals(methodName)) {
			return "0";
		} else if ("delete".equals(methodName)) {
			return "1";
		} else if ("modify".equals(methodName)) {
			return "2";
		} else if ("list".equals(methodName)) {
			return "3";
		} else {
			return "";
		}
	}

}
